package com.netsdk.alarm;

import com.netsdk.lib.ToolKits;
import com.netsdk.lib.NetSDKLib.fMessCallBack;
import com.netsdk.demo.module.LoginModule;

public class AlarmListenModule {

    private static boolean bListening = false;

    /**
     * 启动报警监听
     * @return 是否成功
     */
    public static boolean startListen() {
        if (bListening) {
            return true; // 已经在监听
        }

        AlarmDataCallback alarmDataCallback = new AlarmDataCallback();

        // 设置报警回调
        LoginModule.netsdk.CLIENT_SetDVRMessCallBack(alarmDataCallback, null);

        boolean r = LoginModule.login("192.168.1.100", 37777, "admin", "admin123456");
        System.out.println("登录状态");
        System.out.println(r);
        // 启动报警监听
        if (!LoginModule.netsdk.CLIENT_StartListenEx(LoginModule.m_hLoginHandle)) {
            System.err.printf("CLIENT_StartListenEx Failed!" + ToolKits.getErrorCodePrint());
            return false;
        } else {
            System.out.println("CLIENT_StartListenEx success.");
        }

        bListening = true;
        return true;
    }

    /**
     * 停止报警监听
     * @return 是否成功
     */
    public static boolean stopListen() {
        if (!bListening) {
            return true; // 没有在监听
        }

        if (!LoginModule.netsdk.CLIENT_StopListen(LoginModule.m_hLoginHandle)) {
            System.err.printf("CLIENT_StopListen Failed!" + ToolKits.getErrorCodePrint());
            LoginModule.logout();
            return false;
        } else {
            System.out.println("CLIENT_StopListen success.");
        }

        bListening = false;
        return true;
    }
}